# vim: set ft=sh:

install ()
{
    MODULE_FILE="${TMPDIR}/autodetect_modules"
    #blegh, we'll let /tmp clean itself up
    AUTODETECT="$(auto_modules -e '/scsi/' -e '/block' -e '/fusion/' \
                    -e '/usb/' -e '/ide/' -e '/ieee1394/' -e '/cdrom' \
                    -e '/net/' -e '/pcmcia' -e '/ata' \
                  | grep -v -e 'ata_generic.ko' -e 'ide-generic.ko')"


    #Filesystem detection, use sysfs instead of /proc
    findfs ()
    {
        for blkdev in $(find /dev -type b | grep -v -e /dev/loop -e /dev/ram -e /dev/fd); do
            /sbin/blkid -u filesystem -o value -s TYPE -p "${blkdev}" 2> /dev/null
        done
    }

    if [ ${UID} -eq 0 -o "$(groups | grep disk)" != "" ]; then
        for fs in $(findfs | sort | uniq); do
            allfs="${fs} $(modprobe --set-version ${KERNELVERSION} --resolve-alias ${fs})"
            for mod in ${allfs}; do
                for modfile in $(find "${MODULEDIR}" -type f -name "${mod}.ko"); do
                    if [ -n "${modfile}" ]; then
                        AUTODETECT="${AUTODETECT} ${modfile}"
                    fi
                done
            done
        done

        if [ -e /sbin/mdadm ]; then
            for raidmod in $(/sbin/mdadm -E -s -v /dev/hd* /dev/sd* /dev/rd/* /dev/ida/* /dev/cciss/* /dev/ataraid/* /dev/mapper/* \
                        | awk -Flevel= '{print $2}' | awk '{print $1}'); do
                case "${raidmod}" in
                    raid4|raid5|raid6)
                        AUTODETECT="${AUTODETECT} raid456" ;;
                    *)
                        AUTODETECT="${AUTODETECT} ${raidmod}" ;;
                esac
            done
        fi
    else
        err "User does not have proper permissions to read superblocks, raid and filesystem modules are not detected"
    fi

    for m in ${AUTODETECT}; do
        modname="$(basename ${m%%\.ko})"
        grep "^${modname}$" "${MODULE_FILE}" >/dev/null 2>&1 && continue
        case "${m}" in 
            #*/ieee1394/*) echo -e "sbp2\nsd_mod\nsr_mod" >> "${MODULE_FILE}";;
            *ext3*) echo "jbd" >> "${MODULE_FILE}" ;;
            *ext4*) echo -e "jbd2\nmbcache\ncrc16" >> "${MODULE_FILE}" ;;
            *afs*) echo "rxrpc" >> "${MODULE_FILE}" ;;
            *cramfs*) echo "zlib_inflate" >> "${MODULE_FILE}" ;;
            *isofs*) echo "zlib_inflate" >> "${MODULE_FILE}" ;;
            *msdos*) echo "fat" >> "${MODULE_FILE}" ;;
            *vfat*) echo -e "fat\nnls_cp936" >> "${MODULE_FILE}" ;;
            *ocfs2*) echo -e "ocfs2_dlm\njbd\nocfs2_nodemanager\nconfigfs" >> "${MODULE_FILE}" ;;
        esac
        echo "${modname}" >> "${MODULE_FILE}"
    done

    BINARIES=""
    FILES=""
    SCRIPT=""
}

help ()
{
cat <<HELPEOF
  This hook shrinks your initramdisk to a smaller size 
  by autodetecting your needed modules. Be sure to verify
  included modules are correct and none are missing.
  This hook must be run before other subsystem hooks in
  order to take advantage of auto-detection.  Any hooks
  placed before 'autodetect' will be installed in full.
HELPEOF
}
